import Vue from 'vue'
import VueRouter from 'vue-router'
import store from 'store'
Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history'
})

let routes = []
let req = require.context('../views', true, /^.*\.vue$/)
let page = req.keys()
page.forEach((item, index) => {
  let pagePath = item.replace('.vue', '')
  pagePath = pagePath.replace(/^\./, '')
  // 取第一级目录名称作为模块名
  let parent = pagePath.split('/')[1]
  let component = req(item)
  routes.push({
    path: pagePath,
    name: pagePath,
    component: component,
    meta: {
      parent: parent
    }
  })
})
routes.push({
  path: '/',
  redirect: '/goods/listOfAllGoods'
})
// 商品模块默认页
routes.push({
  path: '/goods',
  redirect: '/goods/listOfAllGoods'
})
// 营销管理模块默认页
routes.push({
  path: '/operational',
  redirect: '/operational/activitiesList'
})
// 确保菜单数据载入
store.dispatch('setMenuTreeModel', function () {
  router.addRoutes(routes)
})

export default router
